package jianda;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

public class Jianda2 {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse("src/jianda/Jianda1.xml");
        NodeList student = doc.getElementsByTagName("student");
        NodeList name = doc.getElementsByTagName("name");
        NodeList course = doc.getElementsByTagName("course");
        NodeList score = doc.getElementsByTagName("score");
        for (int a=0; a< student.getLength(); a++){
            Element stuElement = (Element) student.item(a);
            String Id = stuElement.getAttribute("id");
            System.out.println("id:"+Id);
        }
        for (int b=0; b< name.getLength(); b++){
            Element nameElement = (Element) name.item(b);
            String Name = nameElement.getFirstChild().getNodeValue();
            System.out.println("姓名:"+Name);
        }
        for (int c=0; c< name.getLength(); c++){
            Element nameElement = (Element) course.item(c);
            String courseName = nameElement.getFirstChild().getNodeValue();
            System.out.println("课程名称:"+courseName);
        }
        for (int d=0; d< name.getLength(); d++){
            Element nameElement = (Element) score.item(d);
            String Score = nameElement.getFirstChild().getNodeValue();
            System.out.println("成绩:"+Score);
        }
    }
}
